package com.huika.renrench.control.my.bank.activity;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.huika.lib.ui.widget.dialog.MMAlert;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.my.account.SetPayPwdActivity;
import com.huika.renrench.control.my.bank.adapter.MyBankCardAdapter;
import com.huika.renrench.control.my.certification.activity.CertificationActivity;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.support.bean.BankCardBean;
import com.huika.renrench.view.TitleBarHelper;

/**
 * 
 * @description：我的银行卡
 * @author zc
 * @date 2015年6月2日 上午11:13:03
 */
public class MyBankCardActivity extends BaseAct implements Listener<RequestResult<ArrayList<BankCardBean>>>, OnItemClickListener, OnRefreshListener<ListView> {

	private PullToRefreshListView listview;
	private int pageNo = 1;
	private int pageSize = 1;//目前只需要一张银行卡
	private MyBankCardAdapter adapter;
	private Button confirm;
	private ArrayList<BankCardBean> bankList = new ArrayList<BankCardBean>();
	private String userId;
	private View mylayout;
	private View footView;
	private Button foot_confirm;
	
	@Override
	public void setRootView() {
		setContentView(R.layout.activity_my_bank_card);
	}
	
	@Override
	protected void initData() {
		super.initData();
		TitleBarHelper barHelper = new TitleBarHelper(this, -1);
		barHelper.setLeftMsg("返回");
		barHelper.setTitleMsg("我的银行卡");
		userId = GlobalApp.getInstance().getUserId();
	}
	
	@Override
	protected void initWidget() {
		super.initWidget();
		listview = (PullToRefreshListView) findViewById(R.id.listview);
		confirm = (Button) findViewById(R.id.confirm);
		footView = LayoutInflater.from(this).inflate(R.layout.foot_view_btn, null);
		foot_confirm = (Button) footView.findViewById(R.id.foot_confirm);
		mylayout = findViewById(R.id.mylayout);
		listview.setOnItemClickListener(this);
		listview.setOnRefreshListener(this);
		foot_confirm.setOnClickListener(this);
		adapter = new MyBankCardAdapter(this);
		confirm.setOnClickListener(this);
		listview.setAdapter(adapter);
//		listview.getRefreshableView().addFooterView(footView);
		getMyBankCardList(userId, pageNo, pageSize);
	}
	/**
	 * 
	 * @description：获取我的银行卡列表
	 * @author zc
	 * @date 2015年6月2日 下午5:44:17
	 */
	private void getMyBankCardList(String userId, int pageNo, int pageSize) {
		showLoadingDialog(getString(R.string.common_loading));
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putCommonTypeParam("userId", userId);
		ajaxParams.putCommonTypeParam("pageNo", pageNo);
		ajaxParams.putCommonTypeParam("pageSize", pageSize);
		FormResultRequest<ArrayList<BankCardBean>> request = new FormResultRequest<ArrayList<BankCardBean>>(UrlConstants.GETMYBANKCARDLIST, this, this, new TypeToken<RequestResult<ArrayList<BankCardBean>>>() {
		}.getType());
		request.setRequestParams(ajaxParams);
		executeRequest(request);
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		if(arg0 == 100 && arg1 == 10000) {
			getMyBankCardList(userId, pageNo, pageSize);
		}
	}
	
	private Dialog dialog;
	
	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
			case R.id.confirm:
			case R.id.foot_confirm:
//					showActivity(this, CertificationActivity.class);
					if(warning()) {
						Intent intent = new Intent(this, BankInfoActivity.class);
						intent.putExtra("flag", 0);
						startActivityForResult(intent, 100);
					}
				break;

		}
	}
	
	private boolean warning() {
		if(TextUtils.isEmpty(GlobalApp.userModel.getIdCard()) ||
				TextUtils.isEmpty(GlobalApp.userModel.getRealName())) {
			dialog = MMAlert.createShowCenterDialog(context, "温馨提示", "您还没有实名认证，请先进行实名认证", "忽略", "立即前往", null, new OnClickListener() {
				@Override
				public void onClick(View v) {
					showActivity(MyBankCardActivity.this, CertificationActivity.class);
				}
			});
			dialog.show();
			return false;
		} else if(GlobalApp.userModel.getIsSetPayPwd() == 0) {
			dialog = MMAlert.createShowCenterDialog(context, "温馨提示", "您未设置交易密码，请先设置", "忽略", "前往设置", null, new OnClickListener() {
				@Override
				public void onClick(View v) {
					showActivity(MyBankCardActivity.this, SetPayPwdActivity.class);
				}
			});
			dialog.show();
			return false;
		}
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(this, BankInfoActivity.class);
		intent.putExtra("bankCardBean", bankList.get(arg2 - 1));
		intent.putExtra("flag", 1);
		startActivityForResult(intent, 100);
	}
	
	@Override
	public void onErrorResponse(VolleyError error) {
		super.onErrorResponse(error);
		listview.onRefreshComplete();
	}

	@Override
	public void onResponse(RequestResult<ArrayList<BankCardBean>> arg0) {
		listview.onRefreshComplete();
		dismissLoadingDialog();
		if(arg0.flag == 1) {
			if(arg0.getRs() != null && arg0.getRs().size() > 0) {
				if(listview.getVisibility() == View.GONE) {
					listview.setVisibility(View.VISIBLE);
					mylayout.setVisibility(View.GONE);
				}
				bankList = arg0.getRs();
				adapter.setGroup(bankList);
			} else {
				if(bankList.size() == 0 || bankList.size() == 1) {
					listview.setVisibility(View.GONE);
					mylayout.setVisibility(View.VISIBLE);
				}
			}
		}
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> arg0) {
		getMyBankCardList(userId, pageNo, pageSize);
	}

}
